package com.rongshu.sendmessage.activity.seeImages;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.gyf.immersionbar.ImmersionBar;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.rongshu.sendmessage.R;
import com.rongshu.sendmessage.view.MultiTouchViewPager;
import com.rongshu.sendmessage.view.TitileLayout;

import java.util.List;

public class PictureDisplayActivity extends Activity {

    private String img_url;
    private int index;
    private List<String> mListUri;
    private TextView titleTvLeft;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ImmersionBar.with(this)
                .statusBarColor(R.color.black)
                .statusBarDarkFont(true)
                .init();
        setContentView(R.layout.activity_picture_display);
        initView();
//        initEvent();
    }
    private void initView() {
        img_url = getIntent().getStringExtra("img_url");
        index = getIntent().getIntExtra("index",0);
        boolean isCanDelete = getIntent().getBooleanExtra("isCanDelete",true);
        View titleLayLeft = findViewById(R.id.titleLayLeft);
        titleTvLeft = findViewById(R.id.titleTvLeft);
        titleLayLeft.setOnClickListener(view->finish());
        View titleLayRight=findViewById(R.id.titleLayRight);
        titleLayRight.setOnClickListener(view->{
            Intent intent=new Intent();
            intent.putExtra("index",index);
            setResult(200,intent);
            finish();
        });
        ImageView titleImgRight=findViewById(R.id.titleImgRight);
        titleImgRight.setImageResource(R.mipmap.delete_icon);
        if (isCanDelete){
            titleLayRight.setVisibility(View.VISIBLE);
        }else{
            titleLayRight.setVisibility(View.GONE);
        }
        mListUri=getIntent().getStringArrayListExtra("uris");

//        mPhotoDraweeView = (PhotoDraweeView) findViewById(R.id.photoView);
        MultiTouchViewPager viewPager = findViewById(R.id.view_pager);
        titleTvLeft.setText(index+1+"/"+mListUri.size());
        viewPager.setAdapter(new DraweePagerAdapter(this,mListUri));
        viewPager.setCurrentItem(index);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                index=position;
                titleTvLeft.setText(index+1+"/"+mListUri.size());
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
    public class DraweePagerAdapter extends PagerAdapter {
        private final Context mContext;
        private List<String> mDrawables;

        public DraweePagerAdapter(Context context, List<String> list){
            this.mContext=context;
            this.mDrawables=list;
        }


        @Override
        public int getCount() {
            return mDrawables.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @RequiresApi(api = Build.VERSION_CODES.Q)
        @Override
        public Object instantiateItem(ViewGroup viewGroup, int position) {
//            final PhotoDraweeView photoDraweeView = new PhotoDraweeView(viewGroup.getContext());
//            PipelineDraweeControllerBuilder controller = Fresco.newDraweeControllerBuilder();
//            controller.setUri(mDrawables.get(position));
//            controller.setOldController(photoDraweeView.getController());
//            controller.setControllerListener(new BaseControllerListener<ImageInfo>() {
//                @Override
//                public void onFinalImageSet(String id, ImageInfo imageInfo, Animatable animatable) {
//                    super.onFinalImageSet(id, imageInfo, animatable);
//                    if (imageInfo == null) {
//                        return;
//                    }
//                    photoDraweeView.update(imageInfo.getWidth(), imageInfo.getHeight());
//                }
//            });
//            photoDraweeView.setController(controller.build());
//            try{
//                viewGroup.addView(photoDraweeView, ViewGroup.LayoutParams.MATCH_PARENT,
//                        ViewGroup.LayoutParams.MATCH_PARENT);
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//            return photoDraweeView;
            ImageView imageView=new ImageView(mContext);
            imageView.setImageURI(Uri.parse(mListUri.get(position)));
            viewGroup.addView(imageView,new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT));
            return imageView;
        }
    }
}
